//
//  ViewController.m
//  mytel
//
//  Created by KL on 17/9/4.
//  Copyright © 2017年 hf. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate>
{
    NSArray *tels;
    NSArray *data;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"mytel";
    self.view.backgroundColor = [UIColor grayColor];
    tels = [NSArray new];
   
    tels = @[@"cdbd,15222",@"abb,1200",@"naa,1740",@"huu,1763"];
    data = tels.copy;

    UISearchBar* bar=[[UISearchBar alloc]initWithFrame:CGRectMake(0,70,250,40)];
    bar.delegate = self;
    
    self.tableView.tableHeaderView = bar;
    
    [self.view addSubview:bar];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    NSString *item = tels[indexPath.row];
    NSArray *arr = [item componentsSeparatedByString:@","];
    cell.textLabel.text = arr.firstObject;
    cell.detailTextLabel.text = arr.lastObject;
     return cell;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0) {
        tels = data.mutableCopy;
        return;
    }
    NSMutableArray *res = [NSMutableArray new];
    for (NSString *text in data) {
        NSArray *arr = [text componentsSeparatedByString:@","];
        if ([arr.firstObject containsString:searchText]) {
            [res addObject:text];
        }
        else  if([arr.lastObject containsObject:searchText])
        {
            [res addObject:text];
        }
    }
    tels = res;
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSString *str = tels[indexPath.row];
//    NSArray *arr = [str componentsSeparatedByString:@","];
//    str = arr.lastObject;
//    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",str]]];

    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *telPhone = [UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"呼叫成功");
    }];
    UIAlertAction *sms = [UIAlertAction actionWithTitle:@"发短信" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"正在编辑");
    }];
    [alert addAction:cancel];
    [alert addAction:telPhone];
    [alert addAction:sms];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"拨打" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSString *str = tels[indexPath.row];
        NSArray *arr = [str componentsSeparatedByString:@","];
        str = arr.lastObject;

        NSMutableString *str2=[[NSMutableString alloc] initWithFormat:@"tel://%@",str];
        NSLog(@"%@",str2);
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str2]]];
        [self.view addSubview:callWebview];
    }];
    return @[action];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
